package cn.xfdzx.android.apps.shop.presenter;

import java.util.List;
import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.DissatisfiedReasonBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.bean.PeiSongBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.contract.OrderRemakeContract;
import cn.xfdzx.android.apps.shop.model.OrderRemakeModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;

/**
 * @author zf
 * @date 2020/9/9.
 * Description：
 */
public class OrderRemakePresenter extends BasePresenter<OrderRemakeContract.View> implements OrderRemakeContract.Presenter {

    private OrderRemakeContract.Model model;

    public OrderRemakePresenter() {
        model = new OrderRemakeModel();
    }

    @Override
    public void uploadMoreImg( List<MultipartBody.Part> files) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.uploadMoreImg(files)
                .compose(RxScheduler.<AfterServiceUpLoadMoreImgBean>Flo_io_main())
                .as(mView.<AfterServiceUpLoadMoreImgBean>bindAutoDispose())
                .subscribe(new Consumer<AfterServiceUpLoadMoreImgBean>() {
                    @Override
                    public void accept(AfterServiceUpLoadMoreImgBean bean) throws Exception {
                        mView.onUploadMoreImgSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void uploadSingImg(List<MultipartBody.Part> files) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.uploadSingImg(files)
                .compose(RxScheduler.<AfterServiceUpLoadSingImgBean>Flo_io_main())
                .as(mView.<AfterServiceUpLoadSingImgBean>bindAutoDispose())
                .subscribe(new Consumer<AfterServiceUpLoadSingImgBean>() {
                    @Override
                    public void accept(AfterServiceUpLoadSingImgBean bean) throws Exception {
                        mView.onUploadSingImgSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getDissatisfied() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getDissatisfied()
                .compose(RxScheduler.<DissatisfiedReasonBean>Flo_io_main())
                .as(mView.<DissatisfiedReasonBean>bindAutoDispose())
                .subscribe(new Consumer<DissatisfiedReasonBean>() {
                    @Override
                    public void accept(DissatisfiedReasonBean bean) throws Exception {
                        mView.onGetDissatisfiedSuccess(bean);
                        //mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        //mView.hideLoading();
                    }
                });
    }

    @Override
    public void getRemakeRetail(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getRemakeRetail(map)
                .compose(RxScheduler.<DissatisfiedReasonBean>Flo_io_main())
                .as(mView.<DissatisfiedReasonBean>bindAutoDispose())
                .subscribe(new Consumer<DissatisfiedReasonBean>() {
                    @Override
                    public void accept(DissatisfiedReasonBean bean) throws Exception {
                        mView.onGetRemakeRetailSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getPeisongInfo(String order_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getPeisongInfo(order_id)
                .compose(RxScheduler.<PeiSongBeen>Flo_io_main())
                .as(mView.<PeiSongBeen>bindAutoDispose())
                .subscribe(new Consumer<PeiSongBeen>() {
                    @Override
                    public void accept(PeiSongBeen bean) throws Exception {
                        mView.getPeisongInfoSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getRemakeMarket(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getRemakeMarket(map)
                .compose(RxScheduler.<DissatisfiedReasonBean>Flo_io_main())
                .as(mView.<DissatisfiedReasonBean>bindAutoDispose())
                .subscribe(new Consumer<DissatisfiedReasonBean>() {
                    @Override
                    public void accept(DissatisfiedReasonBean bean) throws Exception {
                        mView.onGetRemakeMarketSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getMyRemakeListRetail(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getMyRemakeListRetail(limit,page)
                .compose(RxScheduler.<MyRemakeListRetailBean>Flo_io_main())
                .as(mView.<MyRemakeListRetailBean>bindAutoDispose())
                .subscribe(new Consumer<MyRemakeListRetailBean>() {
                    @Override
                    public void accept(MyRemakeListRetailBean bean) throws Exception {
                        mView.onGetMyRemakeListRetailSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });

    }

    @Override
    public void getMyRemakeListMarket(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getMyRemakeListMarket(limit,page)
                .compose(RxScheduler.<MyRemakeListMarketBean>Flo_io_main())
                .as(mView.<MyRemakeListMarketBean>bindAutoDispose())
                .subscribe(new Consumer<MyRemakeListMarketBean>() {
                    @Override
                    public void accept(MyRemakeListMarketBean bean) throws Exception {
                        mView.onGetMyRemakeListMarketSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getDeleteRemake(String replyId, int type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        mView.showLoading();

        model.getDeleteRemake(replyId,type)
                .compose(RxScheduler.<MyRemakeListMarketBean>Flo_io_main())
                .as(mView.<MyRemakeListMarketBean>bindAutoDispose())
                .subscribe(new Consumer<MyRemakeListMarketBean>() {
                    @Override
                    public void accept(MyRemakeListMarketBean bean) throws Exception {
                        mView.onGetDeleteRemakeSuccess(bean);
                        mView.hideLoading();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();

                    }
                });

    }

    @Override
    public void getShopRemakeRetail(boolean isRefresh, boolean isLoading, Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getShopRemakeRetail(map)
                .compose(RxScheduler.<ShopRemakeListRetailBean>Flo_io_main())
                .as(mView.<ShopRemakeListRetailBean>bindAutoDispose())
                .subscribe(new Consumer<ShopRemakeListRetailBean>() {
                    @Override
                    public void accept(ShopRemakeListRetailBean bean) throws Exception {
                        mView.onGetShopRemakeRetailSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getShopRemakeMarket(boolean isRefresh, boolean isLoading, Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getShopRemakeMarket(map)
                .compose(RxScheduler.<ShopRemakeListMarketBean>Flo_io_main())
                .as(mView.<ShopRemakeListMarketBean>bindAutoDispose())
                .subscribe(new Consumer<ShopRemakeListMarketBean>() {
                    @Override
                    public void accept(ShopRemakeListMarketBean bean) throws Exception {
                        mView.onGetShopRemakeMarketSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }


}
